{include file="public/toper" /}
<script type="text/javascript" src="/static/js/myajax.js"></script>
<div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="">物料管理</a>
        <a href="">物料列表</a>
    </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">&#xe666;</i></a>
</div>
<div class="x-body">
    <xblock class="demoTable">
        <button class="layui-btn" onclick="x_admin_show('添加物料','{:url(\'Materiel/add\')}','600','800')"><i class="layui-icon"></i>添加</button>
        <button class="layui-btn" id="btn-expand">全部展开</button>
        <button class="layui-btn" id="btn-fold">全部折叠</button>
    </xblock>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="action">
    <button class="layui-btn layui-btn layui-btn-xs"  onclick="x_admin_show('编辑物料','{:url(\'Materiel/add\')}?id={{d.materiel_id}}','600','800')"><i class="layui-icon">&#xe642;</i>编辑</button>
    <button class="layui-btn-danger layui-btn layui-btn-xs del_btn" id="{{d.materiel_id}}" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</button>
</script>
<script>
    layui.config({
        base: '/static/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    }).use(['layer', 'table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var layer = layui.layer;
        var treetable = layui.treetable;

        // 渲染表格
        var renderTable = function () {
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'materiel_id',
                treePidName: 'parent_id',
                treeDefaultClose: true,
                treeLinkage: false,
                elem: '#test',
                url: "{url('Materiel/index')}",
                page: false
                ,limit: 30
                ,limits:[30,60,90,120,150,180,210,240,270,300]
                ,cols: [[
                    {type: 'numbers',width:'80',title:'序号',align: 'center'},
                    {field: 'materiel_id', title: 'ID',width:'100',align: 'center'},
                    {field: 'name', title: '名称',width:'200',align: 'center'},
                    {field: 'code', title: '编码',width:'100',align: 'center'},
                    {field: 'value_y', title: '缺省值y',width:'100',align: 'center'},
                    {field: 'value_x', title: '缺省值x',width:'100',align: 'center'},
                    {field: 'value_z', title: '缺省值z',width:'100',align: 'center'},
                    {field: 'deposit', title: '押金',width:'100',align: 'center'},
                    {field: 'rent', title: '租金',width:'100',align: 'center'},
                    {field: 'settlement', title: '商家结算',width:'100',align: 'center'},
                    {field: 'one_proportion', title: '一级佣金',width:'100',align: 'center'},
                    {field: 'two_proportion', title: '二级佣金',width:'100',align: 'center'},
                    {templet: '#action', title: '操作',width:'150',align: 'center'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };

        renderTable();
        $('#btn-expand').click(function () {
            treetable.expandAll('#test');
        });


        $('#btn-fold').click(function () {
            treetable.foldAll('#test');
        });
        //删除
        table.on('tool(test)', function(obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除【'+data.name+'】吗？',{btnAlign: 'c'}, function(index){
                    var loading = layer.load(1, {shade: [0.1,'#fff'] });
                    $.get("{:url('Materiel/del')}",{id:data.materiel_id},function(res){
                        layer.close(loading);
                        if(res.code==200){
                            layer.msg(res.msg,{time:1000,icon:1});
                            renderTable();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
{include file="public/footer"/}